package com.bj58.groupbuy.action.charts;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bj58.groupbuy.action.util.VelocityTools;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;

public class EChartsUtils {
	private static VelocityTools tools = new VelocityTools();
	/** 报表  echarts曲线图处理
	 * @param rsList 数据查询结果集
	 * @param cols  查询项
	 * @param dicMap 字段中英文字典
	 * @return
	 * @throws Exception
	 */
	public static String transRS2Lines (List<String> rsList ,List<String> cols, Map<String,String> dicMap,int xIndex)throws Exception{
		
		Map<String,Collection<String>> subMap = Maps.newHashMap();//未分类的结果级
		Map<String, Integer> yAxisIndex = Maps.newHashMap();
		
		//对 结果集 按列进行分类收集
		Multimap<Integer, String> tempRsMap = ArrayListMultimap.create();
		int rsSize = rsList.size();
		for(int i  = 0 ;  i < rsSize; i++){
			String[] tempValues = rsList.get(i).split(";");
			for(int j = 0 ; j < tempValues.length ; j++){
				//数据结果处理
				int tempType = Integer.parseInt(tempValues[j].split(",")[1]);
				String realValue = tempValues[j].split(",")[0];
				switch (tempType) {
				case 1:
					realValue  = tools.comIntNOS(realValue);
					break;
				case 2:
					realValue  = tools.comFloat(realValue);
					break;
				case 3:
					realValue  = tools.comRate(realValue);
					if(realValue.contains("%")){
						realValue = realValue.replace("%", "");
					}
					break;
				}
				if("--".equals(realValue)){
					realValue = "0";
				}
				tempRsMap.put(j, realValue);
			}
		}
		
		//根据排位数， 获取对应的结果集
		for(int i = 1 ; i < cols.size() ; i ++){
			String col  = cols.get(i);
			if("groupBy".equals(col)){
				continue;
			}
			String cnName = dicMap.get(col);//中文名 字典是英文 ： 中文类型
			subMap.put(cnName, tempRsMap.get(i));
		}
		
		//按照各指标数量级进行分类
		//取出各项指标节点平均值
		Iterator<String>  it = subMap.keySet().iterator();
		Map<String , Double> colAVGMap = Maps.newHashMap();
		double max = 0;
		while(it.hasNext()){
			String colName  = it.next();
			Collection<String> sList = subMap.get(colName);
			double sum = 0;
			int sSize = sList.size();
			for(String s : sList){
				sum = sum + Double.valueOf(s);
			}
			double avg = sum/sSize;
			max = avg > max ? avg : max;
			colAVGMap.put(colName, avg);
		}
		
		int yAxisCount = 1; //几个Y轴
		for(String colName : colAVGMap.keySet()){
			double now =colAVGMap.get(colName);
			if(max/now > 20){
				yAxisIndex.put(colName, 1);
				yAxisCount = 2;
			}else{
				yAxisIndex.put(colName, 0);
			}
		}
		
		/** 数据分类完毕  开始匹配数据结果 */
		Collection<String> xAxisList =  tempRsMap.get(xIndex); //X轴集合 （时间）
		
		JSONObject finalObject = new JSONObject();
		finalObject.put("legendData", subMap.keySet());
		finalObject.put("xAxisData", xAxisList);
		finalObject.put("yAxisIndexMap", yAxisIndex);
		finalObject.put("yAxisCount", yAxisCount);
		finalObject.put("lineData", subMap);
		return finalObject.toString();
	}
	
	/** 饼图处理
	 * @param rsList 数据查询结果集
	 * @param cols  查询项
	 * @param dicMap  字典是 <英文 ： 中文>
	 * @throws Exception
	 */
	public static String transRS2Pie(List<String> rsList ,List<String> cols, Map<String,String> dicMap)throws Exception{
		JSONArray cate_JSONArray = new JSONArray();	//[{name:"招聘" , value:"1"},{name:"房产" , value:"2"}]
		List<String> nameList = Lists.newArrayList();
		//对 结果集 “按行”  进行分类收集
		int rsSize = rsList.size();
		for(int i  = 0 ;  i < rsSize; i++){
			String[] tempValues = rsList.get(i).split(";");
			
			String name = tempValues[0].split(",")[0];
			String realValue = tempValues[1].split(",")[0];
			int tempType = Integer.parseInt(tempValues[1].split(",")[1]);
			switch (tempType) {
			case 1:
				realValue  = tools.comIntNOS(realValue);
				break;
			case 2:
				realValue  = tools.comFloat(realValue);
				break;
			case 3:
				realValue  = tools.comRate(realValue);
				if(realValue.contains("%")){
					realValue = realValue.replace("%", "");
				}
				break;
			}
			if("--".equals(realValue)){
				realValue = "0";
			}
			JSONObject json = new JSONObject();
			json.put("name",name);
			json.put("value",realValue);
			nameList.add(name);
			cate_JSONArray.add(json);
		}
		
		/** 数据分类完毕  开始匹配数据结果 */
		JSONObject finalObject = new JSONObject();
		finalObject.put("nameList", nameList);
		finalObject.put("indexName", dicMap.get(cols.get(1)));
		finalObject.put("pieData", cate_JSONArray);
		return finalObject.toString();
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
